<template>
    <!--按钮-->
    <div>
        <el-button type="danger" :icon="Plus" @click="SaveBreedingSow">新增</el-button>
        <el-button type="danger" :icon="DocumentAdd">保存</el-button>
        <el-button type="danger" :icon="BottomRight">撤销</el-button>
        <el-button type="danger" :icon="Delete">删除</el-button>
    </div>

    <div style="margin-top: 20px;margin-bottom: 20px;">
        <span>期初日期&nbsp;{{ form[0].openingDate }}</span>
        <span style="margin-left: 100px;">单据号&nbsp;-- --</span>
        <span style="margin-left: 100px;">流水号&nbsp;-- --</span>
    </div>
    <hr size="6" style="background-color:rgb(232.8, 233.4, 234.6)">
    <!--表格-->
    <table>
        <tr>
            <td width="180">序号</td>
            <td width="180">耳号</td>
            <td width="180">栋舍</td>
            <td width="180">栏位</td>
            <td width="180">转种日期</td>
            <td width="180">重量(公斤)</td>
        </tr>
        <tr v-for="(item, index) in form" :key="index">
            <td width="180"><el-input :value="index + 1" /></td>
            <td width="180">
                <el-select v-model="item.basicInforId" placeholder="请选择" clearable>
                    <el-option v-for="basic in basicInforList.filter((x:any)=>x.gender==2 && x.pigTypeId==2 && x.presentstatus==1)" :key="basic.basicInforId" :label="basic.earNumber" :value="basic.basicInforId" />
                </el-select>
            </td>
            <td width="180">
                <el-select v-model="item.bedroomId" placeholder="请选择" @change="handlefield(item.bedroomId, index)"
                    clearable>
                    <el-option v-for="bedroom in bedroomList" :key="bedroom.bedroomId" :label="bedroom.bedroomName" :value="bedroom.bedroomId" />
                </el-select>
            </td>
            <td width="180">
                <el-select v-model="item.fieldId" placeholder="请选择" clearable>
                    <el-option v-for="field in fieldList[index]" :key="field.fieldId" :label="field.fieldName" :value="field.fieldId" />
                </el-select>
            </td>
            <td width="180">
                <el-date-picker v-model="item.dateofConversion" type="date" value-format="YYYY-MM-DD" placeholder="请选择时间" :size="size" />
            </td>
            <td width="180"><el-input v-model="item.weight" /></td>
        </tr>
    </table>
    <!--增行 删行-->
    <div>
        <el-button type="danger" link @click="addRow">+增行</el-button>
        <el-button type="danger" link @click="removeRow" :disabled="form.length <= 0">-删行</el-button>
    </div>
</template>

<script lang="ts" setup>
import { Plus, Delete, DocumentAdd, BottomRight } from '@element-plus/icons-vue'
import { ref, onMounted, reactive } from 'vue'
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from 'vue-router'

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

//创建路由实例
const router = useRouter()
const route = useRoute()

const size = ref<'default' | 'large' | 'small'>('default')

//当前时间
const date = new Date();

//响应式数据 获取栋舍
const condition = reactive({
    pageIndex: 1,
    pageSize: 100,
    bedroomTypeId: 0,
    bedroomName: '',
    personName: ''
})

const form=ref<any>([{
    documentNumber:'',
    serialNumber:'',
    openingDate:date.getFullYear()+'-'+(date.getMonth()+1).toString().padStart(2, '0')+'-'+ date.getDate().toString().padStart(2, '0'),
    basicInforId: '',
    bedroomId: '',
    fieldId: '',
    dateofConversion: '',
    weight: '',
    isBreedingSow: 0,
    createdOwner: localStorage.getItem("userName"),
    createdTime: new Date(),
    // createdTime:date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+''+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(),
    updateOwner: '',
    updatedTime: null,
    examineOwner: '',
    examineTime: null,
}])

//增行 删行
const addRow = () => {
    form.value.push({
      documentNumber:'',
      serialNumber:'',
      openingDate:date.getFullYear()+'-'+(date.getMonth()+1).toString().padStart(2, '0')+'-'+ date.getDate().toString().padStart(2, '0'),
      basicInforId: '',
      bedroomId: '',
      fieldId: '',
      dateofConversion: '',
      weight: '',
      isBreedingSow: 0,
      createdOwner: localStorage.getItem("userName"),
      createdTime:new Date(),
      updateOwner: '',
      updatedTime: null,
      examineOwner: '',
      examineTime: null,
    })
    console.log(form.value)
}
const removeRow = () => {
    if (form.value.length > 0) {
        form.value.pop()
    }
}

//新增
const SaveBreedingSow = () => {
    console.log(form.value)
    const obj = {
        breedingSows: form.value
    }
    axios.post(apiBedinUrl+'/api/BreedingSows/CreateBreedingSow', obj)
        .then(res => {
            if (res.data.code == 0) {
                ElMessage.success("添加种母期初成功");
            } else {
                ElMessage.error("添加种母期初失败");
            }
        })
}

//获取耳号
const basicInforList: any = ref([])
const handlebasicInfor = () => {
    axios.post(apiBedinUrl+'/api/Basics/SearchBasic', {}).then(res => {
        basicInforList.value = res.data.data
    })
}

//获取栋舍
const bedroomList: any = ref([])
const handleBedroom = () => {
    axios.post(apiUrl+'/api/BedRoom/ShowBedroom', {
        pageIndex: condition.pageIndex,
        pageSize: condition.pageSize,
        bedroomTypeId: condition.bedroomTypeId,
        bedroomName: condition.bedroomName,
        personName: condition.personName
    }).then(res => {
        bedroomList.value = res.data.data.list
    })
}

//获取栏位
const fieldList:any=ref([])
const handlefield=(id:number,index:number)=>{
    form.value[index].fieldId=''
    axios.post(apiUrl+'/api/BedRoom/GetBrdroomIdField',{
        bedroomId:id
    }).then(res=>{
        fieldList.value[index]=res.data.data
        console.log("栏位",fieldList.value[index],index)
    })
}

//创建生命周期函数钩
onMounted(() => {
    handlebasicInfor();
    handleBedroom();
})
</script>
